<!--
 * @Author: White
 * @description: 歌单详情
 * @LastEditTime: 2021-09-01 11:59:42
-->

<template>
    <div class="contetn-detail" v-show="!loading">
        <!-- 歌单详情 -->
        <div class="content-header">
            <div class="contetn-coverimg">
                <img :src="detailList.coverImgUrl" alt="">
            </div>
            <div class="content-text">
                <div class="title">
                    <span>歌单</span><div>{{detailList.name}}</div>
                </div>
                <div class="user">
                    <img :src="data.avatarUrl" alt="">
                    <span class="username">{{data.nickname}}</span>
                    <span class="createtime">{{detailList.createTime}}</span>
                </div>
                <div class="play">
                    <span>播放全部</span>
                </div>
                <div class="tag">
                    <div class="tag-name">
                        <div class="tag-item" v-for="(item, index) in detailList.tags" :key="index">
                            <div v-if="item">标签: {{item}}</div>
                            <div v-else>标签：暂无标签</div>
                        </div>
                    </div>
                    <div class="tag-number">
                        <div class="singer">歌曲数: {{detailList.trackCount}}</div>
                        <div>播放数: {{detailList.playCount}}</div>
                    </div>
                    <div class="tag-desc">
                        <span v-if="detailList.description">描述：{{detailList.description}}</span>
                        <span v-else>描述：暂无描述</span>
                    </div>
                </div>
            </div>
        </div>
        <!-- 歌单列表 -->
        <div class="content-list">
            <el-tabs v-model="activeName" @tab-click="tabClick(activeName)">
                <el-tab-pane label="单曲" name="single">
                    <div class="single">
                        <div class="single-header">
                            <div class="single-title">音乐标题</div>
                            <div class="single-author">歌手</div>
                            <div class="single-album">专辑</div>
                            <div class="single-time">时长</div>
                        </div>
                        <div class="single-content">
                            <div class="single-item" v-for="(item, index) in songList" :key="index" 
                            :class="{'active':index%2 != 0}"
                            @click="singlePlay(item.id)">
                                <div class="single-title">
                                    <span class="index">{{index + 1}}</span>
                                    <span class="name">{{item.name}}</span>
                                </div>
                                <div class="single-author">{{item.auther}}</div>
                                <div class="single-album">{{item.album}}</div>
                                <div class="single-time">{{second(item.duration)}}</div>
                            </div>
                        </div>
                    </div>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
    
    <div class="card-wrapper-loading"
        v-show="loading"
        v-loading="loading"
        element-loading-text="载入中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255)">
    </div>
</template>

<script>
import { useStore } from "vuex"
import { reactive, onMounted, toRefs, watch } from 'vue'
import { useRoute } from 'vue-router'
import { getSonglistDetail, getMusicDetail } from '@/api/music'
import { second } from '@/utils/index'
export default {
    setup () {
        const store = useStore()
        const route = useRoute()
        const data = reactive({
            loading: false,
            detailList: [],
            songArrayID: [],
            songList: []
        })
        const userinfo = reactive({
            data: {},
            activeName: 'single'
        })
        const params = reactive({
            id: route.params.id
        })
        
        onMounted(() => {
            getData(Number(route.params.id))
        })
        // 监听路由参数变化
        watch(() => route.params, () => {
            params.id = Number(route.params.id)
            getData()
            data.loading = false
        })
        
        const getData = async() => {
            data.loading = true
            const res = await getSonglistDetail(params)
            if (res.data.code === 200) {
                data.detailList = res.data.playlist
                data.songArrayID = res.data.playlist.trackIds
                userinfo.data = res.data.playlist.creator
            }
            data.songArrayID.map(item => {
                // 根据id数组获取歌曲详情
                getSongList(item.id)
            })
            data.loading = false
        }
        // ID获取歌曲
        const getSongList = async (id) => {
            let params = { ids: id }
            const res = await getMusicDetail(params)
            if(res.data.code === 200) {
                data.songList.push({
                    id: res.data.songs[0].id,
                    name: res.data.songs[0].name,
                    auther: res.data.songs[0].ar[0].name,
                    album: res.data.songs[0].al.name,
                    publishTime: res.data.songs[0].publishTime,
                    duration: res.data.songs[0].dt
                })
            }
        }
        // 单曲播放
        const singlePlay = async(id) => {
            let parmas = { ids: id }
            const res = await getMusicDetail(parmas)
            store.commit("setCurrentSong", res.data)
        }
        return {
            ...toRefs(data),
            ...toRefs(userinfo),
            ...toRefs(params),
            singlePlay,
            second
        }
    }
}
</script>

<style lang="scss" scoped>
.contetn-detail {
    .content-header {
        display: flex;
        align-items: flex-start;
        justify-content: flex-start;
        padding: 16px 0 40px 0;
        .contetn-coverimg {
            width: 200px;
            height: 200px;
            margin-right: 30px;
            img {
                width: 200px;
                height: 200px;
                overflow: hidden;
                border-radius: 8px;
            }
        }
        .content-text {
            flex: 1;
            height: 200px;
            .title {
                display: flex;
                align-items: center;
                margin-bottom: 14px;
                margin-top: 10px;
                span {
                    margin-right: 10px;
                    padding: 2px 4px;
                    color: #d33a31;
                    border: 1px solid #d33a31;
                    border-radius: 4px;
                }
            }
            .user {
                display: flex;
                align-items: center;
                margin-bottom: 14px;
                img {
                    width: 20px;
                    height: 20px;
                    margin-right: 10px;
                }
                .username {
                    font-size: 13px;
                    margin-right: 10px;
                }
                .createtime {
                    font-size: 13px;
                    color: #666;
                }
            }
            .play {
                width: 200px;
                display: flex;
                align-items: center;
                justify-content: center;
                height: 30px;
                border-radius: 50px;
                cursor: pointer;
                background-color: #fc5145;
                color: #fff;
                margin-bottom: 14px;
            }
            .tag {
                font-size: 12px;
                color: #000000;
                .tag-name {
                    margin-bottom: 14px;
                    display: flex;
                }
                .tag-number {
                    display: flex;
                    align-items: center;
                    margin-bottom: 14px;
                    .singer {
                        margin-right: 10px;
                    }
                }
                .tag-desc {
                    line-height: 20px;
                    span {
                        text-overflow: -o-ellipsis-lastline;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        display: -webkit-box;
                        -webkit-line-clamp: 2;
                        line-clamp: 2;
                        -webkit-box-orient: vertical;
                    }
                }
            }
        }
    }
    .content-list {
        :deep(.el-tabs__item.is-active) {
            color: #c3473a;
        }
        :deep(.el-tabs__active-bar) {
            background-color: #c3473a;
        }
        :deep(.el-tabs__item:hover) {
            color: #c3473a;
        }
        :deep(.el-tabs__header) {
            margin: 0;
        }
        .single {
            .single-header {
            display: flex;
            font-size: 13px;
            color: #888;
            height: 38px;
            line-height: 38px;
                .single-title {
                    width: 50%;
                    text-align: left;
                    padding-left: 44px;
                    box-sizing: border-box;
                }
                .single-author {
                    width: 15%;
                }
                .single-album {
                    width: 20%;
                }
                .single-time {
                    width: 15%;
                }
            }
            .single-content {
                color: #666;
                font-size: 12px;
                .active {
                    background-color: #fafafa;
                }
                .single-item {
                    display: flex;
                    flex-wrap: wrap;
                    align-items: center;
                    height: 30px;
                    line-height: 30px;
                    .single-title {
                        width: 50%;
                        text-align: left;
                        white-space: nowrap;  
                        text-overflow:ellipsis;
                        overflow:hidden;
                        .index {
                            display: inline-block;
                            padding: 0px 20px;
                            color: #d3d3d3;
                        }
                    }
                    .single-author {
                        width: 15%;
                        white-space: nowrap;  
                        text-overflow:ellipsis;
                        overflow:hidden;
                    }
                    .single-album {
                        width: 20%;
                        white-space: nowrap;  
                        text-overflow:ellipsis;
                        overflow:hidden;
                    }
                    .single-time {
                        width: 15%;
                    }
                }
            }
        }
        .singer {
            .singer-item {
                display: flex;
                align-items: center;
                height: 80px;
                .singer-img {
                    img {
                        width: 60px;
                        height: 60px;
                        border-radius: 10px;
                    }
                }
                .singer-text {
                    margin-left: 20px;
                    span {
                        font-size: 14px;
                        color: #6c9abc;
                        cursor: pointer;
                    }
                }
            }
        }
        .album {
            .album-item {
                height: 80px;
                display: flex;
                align-items: center;
                cursor: pointer;
                .album-img {
                    width: 50%;
                    display: flex;
                    align-items: center;
                    img {
                        width: 60px;
                        height: 60px;
                        border-radius: 10px;
                        margin-right: 20px;
                    }
                    span {
                        font-size: 14px;
                        color: #000;
                    }
                    .desc {
                        color: #888;
                        padding-left: 10px;
                    }
                }
                .album-author {
                    span {
                        font-size: 12px;
                        color: #6c9abc;
                    }
                }
            }
        }
        .video {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            margin-top: 20px;
            .video-item {
                width: 30%;
                margin-bottom: 10px;
                img {
                    width: 100%;
                    height: 150px;
                    border-radius: 10px;
                }
                .video-title {
                    font-size: 14px;
                    line-height: 30px;
                    color: #333;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }
                .video-desc {
                    font-size: 13px;
                    color: gray;
                }
            }
        }
    }
}
.card-wrapper-loading {
    height: calc(100vh - 420px);
    z-index: 9;
    top: 90px;
    :deep(.el-loading-spinner) {
        display: flex;
        align-items: center;
        justify-content: center;
    }
    :deep(.el-icon-loading) {
        color: #666;
    }
    :deep(.el-loading-text) {
        margin-left: 2px;
        color: #666;
    }
}
</style>